package com.qxzh.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.img.ImgUtil;
import com.qxzh.common.annotation.Anonymous;
import com.qxzh.common.utils.poi.ExcelUtil;
import com.qxzh.system.domain.CertificateManagement;
import com.qxzh.system.service.ICertificateManagementService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qxzh.common.annotation.Log;
import com.qxzh.common.core.controller.BaseController;
import com.qxzh.common.core.domain.AjaxResult;
import com.qxzh.common.enums.BusinessType;
import com.qxzh.common.core.page.TableDataInfo;

/**
 * 证书管理Controller
 * 
 * @author lc
 * @date 2024-05-21
 */
@RestController
@RequestMapping("/business/management")
public class CertificateManagementController extends BaseController
{
    @Autowired
    private ICertificateManagementService certificateManagementService;

    /**
     * 查询证书管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:management:list')")
    @GetMapping("/list")
    public TableDataInfo list(CertificateManagement certificateManagement)
    {
        startPage();
//        QrConfig config = new QrConfig();
        List<CertificateManagement> list = certificateManagementService.selectCertificateManagementList(certificateManagement);
//        List<CompletableFuture> futures = new ArrayList<>();
//        list.forEach(c->{
//            // 通过allOf对多个异步执行结果进行处理
//            CompletableFuture allFuture = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
//                    .whenComplete((v, t) -> {
//                        // 所有CompletableFuture执行完成后进行遍历
//                        futures.forEach(future -> {
//                            synchronized (this) {
//                                // 查询结果合并
//                                String qrCode = QrCodeUtil.generateAsBase64("http://192.168.31.121:5991/detail.html?id="+c.getId(),config, ImgUtil.IMAGE_TYPE_PNG);
//                               c.setQrCode(qrCode);
//                            }
//                        });
//                    });
//            // 阻塞等待所有CompletableFuture执行完成
//            try {
//                allFuture.get();
//            } catch (InterruptedException | ExecutionException e) {
//                throw new RuntimeException(e);
//            }
//        });
        return getDataTable(list);
    }

//    public static void main(String[] args) {
//        QrConfig config = new QrConfig();
//        String qrCode = QrCodeUtil.generateAsBase64("123123123",config, ImgUtil.IMAGE_TYPE_PNG);
//        System.out.println("qrCode = " + qrCode);
//
//    }

    /**
     * 导出证书管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:management:export')")
    @Log(title = "证书管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CertificateManagement certificateManagement)
    {
        List<CertificateManagement> list = certificateManagementService.selectCertificateManagementList(certificateManagement);
        ExcelUtil<CertificateManagement> util = new ExcelUtil<CertificateManagement>(CertificateManagement.class);
        util.exportExcel(response, list, "证书管理数据");
    }

    /**
     * 获取证书管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('business:management:query')")
    @Anonymous
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        CertificateManagement certificateManagement = certificateManagementService.selectCertificateManagementById(id);
//        QrConfig config = new QrConfig();
//        String qrCode = QrCodeUtil.generateAsBase64("http://192.168.31.121:5991/detail.html?id="+certificateManagement.getId(),config, ImgUtil.IMAGE_TYPE_PNG);
//        certificateManagement.setQrCode(qrCode);
        return success(certificateManagement);
    }

    /**
     * 新增证书管理
     */
    @PreAuthorize("@ss.hasPermi('business:management:add')")
    @Log(title = "证书管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CertificateManagement certificateManagement)
    {
        return toAjax(certificateManagementService.insertCertificateManagement(certificateManagement));
    }

    /**
     * 修改证书管理
     */
    @PreAuthorize("@ss.hasPermi('business:management:edit')")
    @Log(title = "证书管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CertificateManagement certificateManagement)
    {
        return toAjax(certificateManagementService.updateCertificateManagement(certificateManagement));
    }

    /**
     * 删除证书管理
     */
    @PreAuthorize("@ss.hasPermi('business:management:remove')")
    @Log(title = "证书管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(certificateManagementService.deleteCertificateManagementByIds(ids));
    }
}
